package cn.icanci.loopstack.bic.web.controller;

import java.util.List;

import javax.annotation.Resource;

import org.springframework.web.bind.annotation.*;

import cn.icanci.loopstack.bic.biz.service.EdgeService;
import cn.icanci.loopstack.bic.common.model.TextValue;
import cn.icanci.loopstack.bic.common.model.base.EdgeVO;
import cn.icanci.loopstack.bic.common.result.R;
import cn.icanci.loopstack.bic.web.form.EdgeQueryForm;
import cn.icanci.loopstack.bic.web.mapper.EdgeWebMapper;
import cn.icanci.loopstack.bic.web.model.Edge;

/**
 * @author icanci
 * @since 1.0 Created in 2023/09/11 23:21
 */
@RestController
@RequestMapping("/bicAdmin/edge")
public class EdgeController {
    @Resource
    private EdgeWebMapper edgeWebMapper;
    @Resource
    private EdgeService   edgeService;

    // ================================ CRUD ================================
    @PostMapping("query")
    public R query(@RequestBody EdgeQueryForm form) {
        return R.builderOk().data("queryPage", edgeService.queryPage(edgeWebMapper.web2vo(form.getEdge()), form.getPaginator().getCurrentPage(), form.getPaginator().getPageSize()))
            .build();
    }

    @PostMapping("save")
    public R save(@RequestBody Edge edge) {
        edgeService.save(edgeWebMapper.web2vo(edge));
        return R.builderOk().build();
    }

    @GetMapping("validateName/{systemUuid}/{name}")
    public R validateName(@PathVariable("systemUuid") String systemUuid, @PathVariable("name") String name) {
        EdgeVO edge = edgeService.queryBySystemUuidAndName(systemUuid, name);
        return R.builderOk().data("result", edge == null).build();
    }

    @GetMapping("validateKey/{systemUuid}/{key}")
    public R validateKey(@PathVariable("systemUuid") String systemUuid, @PathVariable("key") String key) {
        EdgeVO edge = edgeService.queryBySystemUuidAndKey(systemUuid, key);
        return R.builderOk().data("result", edge == null).build();
    }
    // ================================ Load Selectors ================================

    @GetMapping("loadSelector/{systemUuid}")
    public R loadSelector(@PathVariable("systemUuid") String systemUuid) {
        List<TextValue> textValues = edgeService.loadSelector(systemUuid);
        return R.builderOk().data("textValues", textValues).build();
    }
}
